package com.atguigu.threadtest;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * ClassName: ThreadCommunication
 * Package: com.atguigu
 * Description:
 *
 * @author: fu-wang
 * @create: 2024/10/28 20:39
 * version: 1.0
 */
public class ThreadCommunication
/**
 * 多线程模版（上） 高内聚 低耦合 线程操作资源类
 * 多线程模版（中） 判断 干活 通知
 *  while有效防止虚假唤醒
 */
{
    private Integer num = 0;
    private final Lock lock = new ReentrantLock();//初始化lock锁
    private final Condition condition = lock.newCondition();

    //++
    public void increment() throws InterruptedException {
        //加入锁
        lock.lock();
        try {
            //判断
            while (num!=0){
                condition.await();
            }
            //干活
            num++;
            System.out.println(Thread.currentThread().getName()+"\t"+num);
            //通知
            condition.signalAll();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            //释放锁
            lock.unlock();
        }

    }
    //--
    public void decrement() throws InterruptedException {
        lock.lock();
        try {
            //判断
            while (num!=1){
                condition.await();
            }
            //干活
            num--;
            System.out.println(Thread.currentThread().getName()+"\t"+num);
            //通知
            condition.signalAll();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }

    }
}
class ThreadCommunicationTest{
    public static void main(String[] args) {
        ThreadCommunication communication = new ThreadCommunication();
        new Thread(()->{
            for (int i = 0; i < 50; i++) {
                try {
                    communication.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
                    },"thread increment 1").start();
        new Thread(()->{
            for (int i = 0; i < 50; i++) {
                try {
                    communication.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"thread increment 2").start();

        new Thread(()->{
            for (int i = 0; i < 50; i++) {
                try {
                    communication.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
                    },"thread decrement 1").start();
        new Thread(()->{
            for (int i = 0; i < 50; i++) {
                try {
                    communication.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"thread decrement 2").start();
    }
}
